﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MarginTrading.Entity;
using MarginTrading.Services;
using MarginTrading.Common;


namespace MarginTrading.site
{
    public partial class ChangePass : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null)
            {
                Response.Redirect("Login.aspx");
            }
            PlaceHolder1.Visible = false;
            PlaceHolder2.Visible = true;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            PlaceHolder1.Visible = false;
            PlaceHolder2.Visible = true;

            if (TextBoxConfirmPass.Text == "" || TextBoxCurrentPass.Text == "" || TextBoxNewPass.Text == "")
            {
                LabelErrMsg.Text = "Enter necessary details";
                return;
            }

            if (TextBoxConfirmPass.Text != TextBoxNewPass.Text)
            {
                LabelErrMsg.Text = "New password does not match confirmation password";
                return;
            }

            UserAccount User = null;
            try
            {
                User = UserAccountServices.GetUserAccountByUserName(Session["UserName"].ToString());
            }
            catch (System.Exception ex)
            {
                LabelErrMsg.Text = "Site temporally out of service";
                return;
            }

            if (User == null)
            {
                LabelErrMsg.Text = "Failed to find user";
                //MessageBox.Show("Invalid Username or Password", "Invalid Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                String sHashedPass = MD5Calculator.CalculateMD5Hash(TextBoxCurrentPass.Text);

                if (sHashedPass == User.Password)
                {
                    User.Password = MD5Calculator.CalculateMD5Hash(TextBoxNewPass.Text);
                    UserAccountServices.Update(User);

                    //Response.Redirect("MTStatusReport.aspx");
                    PlaceHolder1.Visible = true;
                    PlaceHolder2.Visible = false;
                }
                else
                {
                    LabelErrMsg.Text = "Wrong password";
                    //MessageBox.Show("Invalid Username or Password", "Invalid Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}
